package com.kgc.controller;

import com.kgc.config.annotation.RequestPermission;
import com.kgc.service.AddGoodsStockService;
import com.kgc.service.ArrivalRemindService;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created on 2021/6/23.
 * Author: lzy
 * Description: 添加商品库存入口
 */
@RestController
@Slf4j
@Api(tags = "管理员添加商品库存入口")
public class AddGoodsStockController {


    @Autowired
    private AddGoodsStockService addGoodsStockService;


    @Autowired
    private ArrivalRemindService arrivalRemindService;

    /**
     * @ClassName: AddGoodsStockController
     * @Description 管理员添加商品库存
     * @Author lzy
     * @Date 2021/6/23
     */
    @PostMapping("/addStocks")
    @ApiOperation(value = "添加商品库存", notes = "需要传入商品id,以及商品需要添加的库存")
    @ApiResponses({
            @ApiResponse(code = 6995, message = "该商品不存在"),
            @ApiResponse(code = 6994, message = "添加数据库失败"),
            @ApiResponse(code = 0000, message = "提醒用户到货成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", required = true, value = "商品编号")
            , @ApiImplicitParam(name = "goodsStock", required = true, value = "添加的库存")})
    public ReturnResult<String> addGoodsStock(@RequestParam String goodsId,
                                              @RequestParam Integer goodsStock) {

        // 判断是否存在该商品
        if (addGoodsStockService.judgeGoodsExists(goodsId)) {
            // 添加商品库存到数据
            if (addGoodsStockService.queryUpdateGoodsStock(goodsId, goodsStock)) {
                // 发送信息给用户进行
                arrivalRemindService.sendSmsToUserGoodsHasStock(goodsId);
                return ResultBuildUtil.success("添加库存成功，并给设置提醒用户发送提醒");
            } else {
                return ResultBuildUtil.fail("6994", "添加数据库失败");
            }
        } else {
            return ResultBuildUtil.fail("6995", "该商品不存在！");
        }
    }


}
